
package lib::Cfg;

use strict;

sub new { # ( default constructor )
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{'CONFIGURATION'} = {
		'EMPTY' => 0
	};

	$self->{'DATA'} = {
		'USER_MSG' => 1,
		'PORT' => 'COM1',
		'DATABITS' => 8,
		'BAUDRATE' => 19200,
		'PARITY' => "none",
		'STOPBITS' => 1,
		'HANDSHAKE' => "none",
		'BUFFERS' => 4096,
		'IOTIMING' => 13,
	};

	bless($self, $class);
	return $self;
}


# ########## CONFIGURATION METHODS 
# ########## CONFIGURATION METHODS 
# ########## CONFIGURATION METHODS 
#
sub getCfgValue { # 

	my $self = shift;
	my $data = $self->{'CONFIGURATION'};
	my $key = shift;
	my $a;
	undef($a);

	$a = $data->{$key};

	defined($a) or warn "lib::Data - Invalid component number $key\n";

	return $a;
}


sub setCfgValue { # 

	my $self = shift;
	my $data = $self->{'CONFIGURATION'};
	my $key = shift;
	my $val = shift;
	my $a;
	undef($a);

	$a = $data->{$key};

	defined($a) or warn "lib::Data - Configuration item >$key< not initialized yet.\n";

	$data->{$key} = $val;

	return 1;
}


sub getCfgItemList {

	my $self = shift;
	my $data = $self->{'CONFIGURATION'};
	my @a;
	undef(@a);

	@a = keys(%$data);

	defined(@a) or warn "lib::Data - Invalid list\n";

	return \@a;
}


sub saveCfg {

	my $self = shift;
	my $data = $self->{'CONFIGURATION'};

	open CF, ">controller.ini" or warn "lib::Data - casomira.ini nelze otevrit pro zapis - $!\n";

	foreach my $item (sort(keys(%$data))) { 
		print CF ";item\n";
		print CF "${item}\n";
		print CF "$data->{$item}\n";
	}

	close CF;
}


sub loadCfg{

	my ($key, $value);
	my $self = shift;
	my $data = $self->{'CONFIGURATION'};

	open CF, "<controller.ini" or warn "lib::Data - casomira.ini nenalezen\n";

	while(<CF>) { # strip off the comment line that begins with ';' symbol.
		$key = <CF>;
		chomp($key);
		$value = <CF>;
		chomp($value);
		
		$data->{$key} = $value;
	}

	close CF;
}


# ########## DEVICE METHODS 
# ########## DEVICE METHODS 
# ########## DEVICE METHODS 

sub getValue { # 

	my $self = shift;
	my $data = $self->{'DATA'};
	my $key = shift;
	my $a;
	undef($a);

	$a = $data->{$key};

	defined($a) or warn "lib::Data - Invalid component number $key\n";

	return $a;
}


sub setValue { # 

	my $self = shift;
	my $data = $self->{'DATA'};
	my $key = shift;
	my $val = shift;
	my $a;
	undef($a);

	$a = $data->{$key};

	defined($a) or warn "lib::Data - Device configuration item >$key< not initialized yet.\n";

	$data->{$key} = $val;

	return 1;
}


sub getItemList {

	my $self = shift;
	my $data = $self->{'DATA'};
	my @a;
	undef(@a);

	@a = keys(%$data);

	defined(@a) or warn "lib::Data - Invalid list\n";

	return \@a;
}


sub save{

	my $self = shift;
	my $data = $self->{'DATA'};

	open CF, ">device.ini" or warn "lib::Data - device.ini nelze otevrit pro zapis - $!\n";

	foreach my $item (sort(keys(%$data))) { 
		print CF ";item\n";
		print CF "${item}\n";
		print CF "$data->{$item}\n";
	}

	close CF;
}


sub load{

	my ($key, $value);
	my $self = shift;
	my $data = $self->{'DATA'};

	open CF, "<device.ini" or die "lib::cfg - device.ini missing";

	while(<CF>) { # strip off the comment line that begins with ';' symbol.
		$key = <CF>;
		chomp($key);
		$value = <CF>;
		chomp($value);
		
		$data->{$key} = $value;
	}

	close CF;
}


sub rt{
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(shift);
	
	return sprintf( "%2.2u.%2.2u.%4.4u %2.2u:%2.2u:%2.2u", $mday, $mon+1, $year+1900, $hour, $min, $sec);
}

1;

